package com.blacktry.controller;


import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.AdminRole;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.service.AdminRoleService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-06-25
 */
@Api(value="用户角色类模块", tags = "用户角色管理接口")
@RestController
@RequestMapping("/t-admin-role")
public class AdminRoleController {

    @Autowired
    private AdminRoleService adminRoleService;

    @OperLog(operModul = "角色管理-为某个用户分配多个角色",operType = OprLogConst.ADD,operDesc = "为某个用户分配多个角色")
    @ApiOperation("为某个用户分配多个角色")
    @GetMapping("/addRoleForUser/{id}")
    public ApiResult addRoleForUser(@PathVariable("id")Integer id, String[] ids) {
        for (String roleId : ids) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(id);
            adminRole.setRoleId(Integer.valueOf(roleId));
            adminRoleService.save(adminRole);
        }
        return ApiResultHandler.buildApiResult(200, "成功");
    }

    @OperLog(operModul = "角色管理-删除某个用户的角色",operType = OprLogConst.DELETE,operDesc = "删除某个用户的角色")
    @ApiOperation("删除某个用户的角色")
    @DeleteMapping("/deleteAdminRole/{id}")
    public ApiResult deleteAdminRole(@PathVariable("id")Integer id) {
        return ApiResultHandler.buildApiResult(200, "成功",adminRoleService.removeById(id));
    }
}

